package org.jeecg.modules.lab.items.task.service.impl;


import com.aliyun.oss.ServiceException;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.lab.entrust.entity.LabEntrustItems;
import org.jeecg.modules.lab.entrust.entity.LabEntrustSample;
import org.jeecg.modules.lab.entrust.entity.LabWentrust;
import org.jeecg.modules.lab.entrust.entity.LabWsupervise;
import org.jeecg.modules.lab.entrust.mapper.LabEntrustItemsMapper;
import org.jeecg.modules.lab.entrust.mapper.LabWentrustMapper;
import org.jeecg.modules.lab.entrust.mapper.LabWsuperviseMapper;
import org.jeecg.modules.lab.items.task.mapper.LabTaskAllotMapper;
import org.jeecg.modules.lab.items.task.service.ILabTaskAllotService;
import org.jeecg.modules.lab.items.vo.*;
import org.jeecg.modules.lab.utensil.entity.MonthList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 委托、监督任务信息
 * @Author: jeecg-boot
 * @Date: 2023-07-20
 * @Version: V1.0
 */
@Service
public class LabTaskAllotServiceImpl extends ServiceImpl<LabTaskAllotMapper, LabTaskAllotPage> implements ILabTaskAllotService {

    @Autowired
    private LabTaskAllotMapper labTaskAllotMapper;

    @Autowired
    private LabEntrustItemsMapper labEntrustItemsMapper;

    @Autowired
    private LabWentrustMapper labWentrustMapper;

    @Autowired
    private LabWsuperviseMapper labWsuperviseMapper;


    @Override
    public List<LabTaskAllotPage> getWTaskLists(String entName, String sampleType, String allotStatus, String billStatus, String auditId, String ratifyId, String userId,String handlDate,String handler) {
        return labTaskAllotMapper.getWTaskList(entName, sampleType, allotStatus, billStatus, auditId, ratifyId, userId,handlDate,handler);
    }

    @Override
    public List<LabTaskAllotPage> getWReportLists(String entName, String sampleType, String allotStatus, String billStatus, String chiefId, String auditId, String ratifyId, String userId) {
        return labTaskAllotMapper.getWReportList(entName, sampleType, allotStatus, billStatus, chiefId, auditId, ratifyId, userId);
    }

    @Override
    public LabTaskUserPage getAutoUser(String mId) {
        List<LabTaskUserPage> labTaskUserPages = labTaskAllotMapper.getAllotUsers(mId);
        if (labTaskUserPages == null) {
            throw new ServiceException("没有可用的实验人员！");
        }
        LabTaskUserPage labTaskUserPage = labTaskUserPages.get(0);
        return labTaskUserPage;
    }

    @Override
    public List<LabTaskUserPage> getAutoUserList(String mId) {
        List<LabTaskUserPage> labTaskUserPages = labTaskAllotMapper.getAllotUsers(mId);
        if (labTaskUserPages == null) {
            throw new ServiceException("没有可用的实验人员！");
        }
        return labTaskUserPages;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateMain(LabTaskItemsPage labTaskItemsPage) {

        String id = labTaskItemsPage.getId();
        String entrust_Type = labTaskItemsPage.getEntrustType();
        if (entrust_Type.equals("委托检验")) {
            LabWentrust labWentrust = labWentrustMapper.selectById(id);
            if (labWentrust == null) {
                throw new ServiceException("未找到对应数据！");
            }
            labWentrust.setAllotStatus("已分配");
            labWentrust.setSynFlag("1");
            labWentrust.setEditorChief(labTaskItemsPage.getEditorChief());
            labWentrust.setEditorChiefId(labTaskItemsPage.getEditorChiefId());
            labWentrustMapper.updateById(labWentrust);

        } else if (entrust_Type.equals("监督检验")) {
            LabWsupervise labWsupervise = labWsuperviseMapper.selectById(id);
            if (labWsupervise == null) {
                throw new ServiceException("未找到对应数据！");
            }
            labWsupervise.setAllotStatus("已分配");
            labWsupervise.setSynFlag("1");
            labWsupervise.setEditorChief(labTaskItemsPage.getEditorChief());
            labWsupervise.setEditorChiefId(labTaskItemsPage.getEditorChiefId());
            labWsuperviseMapper.updateById(labWsupervise);
        }

        //2更新实验项目数据
        List<LabEntrustItems> labEntrustItemsList = labTaskItemsPage.getLabEntrustItems();
        if (labEntrustItemsList != null && labEntrustItemsList.size() > 0) {
            for (LabEntrustItems entity : labEntrustItemsList) {
                entity.setItemStatus("待实验");
                entity.setSynFlag("1");
                labEntrustItemsMapper.updateById(entity);
            }
        }


    }

    @Override
    public List<LabCheckItemsPage> getCheckItems(String userId, String auditId, String entrustType, String entName, String sampleType, String sampleSort, String itemStatus,String pubDate) {
        return labTaskAllotMapper.getCheckItems(userId, auditId, entrustType, entName, sampleType, sampleSort, itemStatus,pubDate);
    }


    @Override
    public LabCheckItemsPage getItems(String taskItemId, String entrustId) {
        LabCheckItemsPage items = labTaskAllotMapper.getOneItems(taskItemId, entrustId);
        return items;
    }

    /**
     * 分配到检测项目
     * @param labEntrustItemss
     */
    @Override
    public void updateMainKy(LabEntrustItems labEntrustItemss) {
        labEntrustItemss.setItemStatus("待实验");
        labEntrustItemss.setSynFlag("1");
        labEntrustItemsMapper.updateById(labEntrustItemss);
    }

    @Override
    public String getAllTestUsers(String entrustId) {
        return labTaskAllotMapper.getAllTestUsers(entrustId);
    }

    @Override
    public String getTestDepart(String entrustId) {
        String dep =labTaskAllotMapper.getTestDepart(entrustId);
        if(dep == null ){
            dep ="";
        }
        return dep;
    }

    @Override
    public LabSamItemPage getWaitSamNum(String userId) {
        return labTaskAllotMapper.getWaitSamNum(userId);
    }

    @Override
    public List<LabEntrustSample> getWaitSamList(String userId) {
        return labTaskAllotMapper.getWaitSamList(userId);
    }

    @Override
    public LabSamItemPage querySamItemCount(String date,String checkType,String startTime,String endTime) {
        return labTaskAllotMapper.samAndItemCount(date,checkType,startTime,endTime);
    }

    @Override
    public Map<String, Object> querySamItemYear(String year) {

        Map<String, Object> map = new HashMap<>();
        Map<String, Object> mapNum = new HashMap<>();//每月检测项目数
        Map<String, Object> mapItem = new HashMap<>();//每月实验时间

        List<String> numMonth = new ArrayList<>();
        List<String> numValue = new ArrayList<>();
        List<MonthList> monthLists = labTaskAllotMapper.samNumMonth(year);
        monthLists.forEach(e -> {
            numMonth.add(e.getMonth() + "月");
            numValue.add(e.getCount());
        });
        mapNum.put("xAxis", numMonth);
        mapNum.put("yAxis", numValue);

        List<String> itemMonth = new ArrayList<>();
        List<String> itemValue = new ArrayList<>();
        List<MonthList> lists = labTaskAllotMapper.itemNumMonth(year);
        lists.forEach(e -> {
            itemMonth.add(e.getMonth() + "月");
            itemValue.add(e.getCount());
        });
        mapItem.put("xAxis", itemMonth);
        mapItem.put("yAxis", itemValue);
        map.put("samNum", mapNum);
        map.put("itemNum", mapItem);
        return map;
    }

    @Override
    public List<LabAdutiUserPage> queryAduitUsers(String tId) {
        return labTaskAllotMapper.getAduitUsers(tId);
    }

    @Override
    public List<LabItemsUsersPage> getItemToUsers(List<LabEntrustItems> labEntrustItems) {
        List<LabItemsUsersPage> labItemsUsers = new ArrayList<>();
        for(LabEntrustItems item:labEntrustItems){
            String itemId = item.getItemId();
            String basisId = item.getBasisId();
            LabItemsUsersPage itemsUser = labTaskAllotMapper.getUserSkill(itemId,basisId);
            if(itemsUser != null){
                labItemsUsers.add(itemsUser);
            }
        }
        return labItemsUsers;
    }

    @Override
    public List<LabItemsUsersPage> getItemToUserSkill(String itemId,String basisId,String userName) {
        return labTaskAllotMapper.getUserSkillList(itemId,basisId,userName);
    }

    @Override
    public List<LabItemsUsersPage> setLabUserTree(List<LabItemsUsersPage> labTaskUserTree) {
        LabItemsUsersPage labTaskUserA = new LabItemsUsersPage();
        labTaskUserA.setId("1");
        labTaskUserA.setUserName("油田化学检验室(三组)");
        labTaskUserTree.add(labTaskUserA);
        LabItemsUsersPage labTaskUserB = new LabItemsUsersPage();
        labTaskUserB.setId("2");
        labTaskUserB.setUserName("腐蚀与防护检验室(二组)");
        labTaskUserTree.add(labTaskUserB);
        LabItemsUsersPage labTaskUserC = new LabItemsUsersPage();
        labTaskUserC.setId("3");
        labTaskUserC.setUserName("天然气净化检验室(一组)");
        labTaskUserTree.add(labTaskUserC);
        LabItemsUsersPage labTaskUserD = new LabItemsUsersPage();
        labTaskUserD.setId("4");
        labTaskUserD.setUserName("其他");
        labTaskUserTree.add(labTaskUserD);
        return labTaskUserTree;
    }

    @Override
    public void updateEntrustItems(LabEntrustItems labEntrustItems) {
        labEntrustItems.setSynFlag("1");
        labEntrustItemsMapper.updateById(labEntrustItems);
    }

    @Override
    public List<LabTaskAllotPage> getRecordReportList(String entName, String sampleType, String allotStatus, String billStatus, String chiefId, String auditId, String ratifyId, String userId) {
        return labTaskAllotMapper.getRecordReportList(entName, sampleType, allotStatus, billStatus, chiefId, auditId, ratifyId, userId);
    }


}
